package com.cy.web.action.scenic;

import java.io.File;
import java.io.Serializable;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.cy.bean.scenic.Scenic;
import com.cy.bean.uploadfile.UploadFile;
import com.cy.service.scenic.ScenicService;
import com.cy.service.uploadfile.UploadFileService;
import com.cy.utils.SysParam;
import com.cy.web.action.BaseAction;
import com.opensymphony.xwork2.ActionContext;

/**
 * 景点管理控制器
 * @author CY
 *
 */
@Controller
@Scope("prototype")
public class ScenicManageAction extends BaseAction {

	@Resource
	private ScenicService scenicService;
	@Resource
	private UploadFileService uploadFileService;
	private Scenic scenic;
	private File scenic_img;
	private String scenic_imgContentType;
	private String scenic_imgFileName;
	
	/**
	 * 删除景点
	 * @return
	 */
	public String delete() {
		if(getIds() != null && getIds().length > 0) {
			scenicService.delete((Serializable[])getIds());
			ActionContext.getContext().put("message", "删除成功");
		} else {
			ActionContext.getContext().put("message", "请选择要删除的文件");
		}
		ActionContext.getContext().put("url", SysParam.read("scenicList"));
		return "dialog";
	}
	
	/**
	 * 更新景点
	 * @return
	 */
	public String update() {
		try {
			if (scenic_img != null) {
				String uploadPath = ServletActionContext.getServletContext()
						.getRealPath("/");
				UploadFile uploadFile = new UploadFile(scenic_imgFileName,
						scenic_imgContentType, this.scenic_img);
				uploadFileService.save(uploadPath, uploadFile);
				scenic.setScenic_img(uploadFile);
			} else {
				scenic.setScenic_img(scenicService.find(scenic.getScenic_id()).getScenic_img());
			}
			System.out.println(scenic.getScenic_id());
			scenicService.update(scenic);
			ActionContext.getContext().put("message", "景点更新成功");
		} catch(Exception e) {
			ActionContext.getContext().put("message", "景点更新失败");
			e.printStackTrace();
		}
		ActionContext.getContext().put("url", SysParam.read("scenicList"));
		return "dialog";
	}
	
	/**
	 * 编辑UI
	 * @return
	 */
	public String editUI() {
		scenic = scenicService.find(scenic.getScenic_id());
		ActionContext.getContext().put("scenic", scenic);
		return "editUI";
	}
	
	/**
	 * 添加景点
	 * @return
	 */
	public String add() {
		try {
			if (scenic_img != null) {
				String uploadPath = ServletActionContext.getServletContext()
						.getRealPath("/");
				UploadFile uploadFile = new UploadFile(scenic_imgFileName,
						scenic_imgContentType, this.scenic_img);
				uploadFileService.save(uploadPath, uploadFile);
				scenic.setScenic_img(uploadFile);
			}
			scenicService.save(scenic);
			ActionContext.getContext().put("message", "景点发布成功");
		} catch(Exception e) {
			ActionContext.getContext().put("message", "景点发布失败");
			e.printStackTrace();
		}
		ActionContext.getContext().put("url", SysParam.read("addScenicUI"));
		return "dialog";
	}

	public Scenic getScenic() {
		return scenic;
	}

	public void setScenic(Scenic scenic) {
		this.scenic = scenic;
	}

	public File getScenic_img() {
		return scenic_img;
	}

	public void setScenic_img(File scenic_img) {
		this.scenic_img = scenic_img;
	}

	public String getScenic_imgContentType() {
		return scenic_imgContentType;
	}

	public void setScenic_imgContentType(String scenic_imgContentType) {
		this.scenic_imgContentType = scenic_imgContentType;
	}

	public String getScenic_imgFileName() {
		return scenic_imgFileName;
	}

	public void setScenic_imgFileName(String scenic_imgFileName) {
		this.scenic_imgFileName = scenic_imgFileName;
	}

}
